//
//  ZCCommentCell.m
//  百思不得姐
//
//  Created by Ning Xie on 16/9/2.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCCommentCell.h"
#import "ZCCommentModel.h"
#import "ZCVerticalButton.h"


#define defaultMargin 10

@interface ZCCommentCell ()

@property (weak, nonatomic) UIImageView *headerImage;
@property (weak, nonatomic) UIImageView *vipImage;
@property (weak, nonatomic) UILabel *nameLabel;
@property (weak, nonatomic) UIImageView *sexView;
@property (weak, nonatomic) UILabel *commentLab;
@property (weak, nonatomic) ZCVerticalButton *likeButton;


@end
@implementation ZCCommentCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    UIImageView *headerImage = [[UIImageView alloc]init];
    self.headerImage = headerImage;
    headerImage.layer.cornerRadius = 15;
    headerImage.layer.masksToBounds = YES;
    [self.contentView addSubview:headerImage];
    
    
    UIImageView *vipImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Profile_AddV_authen"]];
    self.vipImage = vipImage;
    [self.contentView addSubview:vipImage];
    
    
    UIImageView *sexView = [[UIImageView alloc]init];
    self.sexView = sexView;
    [self.contentView addSubview:sexView];
    
 
    UILabel *nameLabel = [UILabel labelWithText:nil textColor:[UIColor grayColor] fontSize:10 textAlignment:NSTextAlignmentLeft];
    self.nameLabel = nameLabel;
    [self.contentView addSubview:nameLabel];
    
    
    ZCVerticalButton *likeButton = [[ZCVerticalButton alloc]init];
    [likeButton setImage:[UIImage imageNamed:@"commentLikeButton"] forState:UIControlStateNormal];
    [likeButton setImage:[UIImage imageNamed:@"commentLikeButtonClick"] forState:UIControlStateSelected];
    [likeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [likeButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    likeButton.titleLabel.font = [UIFont systemFontOfSize:10];
    likeButton.adjustsImageWhenHighlighted = NO;
    self.likeButton = likeButton;
    [self.contentView addSubview:likeButton];

    
    UILabel *commentLab = [UILabel labelWithText:nil textColor:[UIColor blackColor] fontSize:13 textAlignment:NSTextAlignmentLeft];
    self.commentLab = commentLab;
    [self.contentView addSubview:commentLab];
    
    
    
    
    [headerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self.contentView).offset(defaultMargin);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
    }];
    
    [vipImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(headerImage);
        make.bottom.equalTo(headerImage);
        make.width.height.mas_equalTo(10);
    }];
    
    [sexView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerImage);
        make.leading.equalTo(headerImage.mas_trailing).offset(defaultMargin);
        make.width.height.mas_equalTo(15);
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerImage);
        make.leading.equalTo(sexView.mas_trailing);
    }];
    
    [likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel);
        make.trailing.equalTo(self.contentView).offset(-20);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(30);
    }];
    
    [commentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sexView.mas_bottom);
        make.leading.equalTo(sexView);
        make.trailing.equalTo(likeButton.mas_leading).offset(-defaultMargin);

    }];
    
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.leading.equalTo(self);
        make.trailing.equalTo(self);
        make.bottom.equalTo(commentLab.mas_bottom).offset(defaultMargin);

    }];
    
   

}

-(void)setModel:(ZCCommentModel *)model{
    _model = model;
    
    [self.headerImage sd_setImageWithURL:[NSURL URLWithString:model.user.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.nameLabel.text = model.user.username;
    self.sexView.image = [model.user.sex isEqualToString:@"m"]?[UIImage imageNamed:@"Profile_manIcon"]:[UIImage imageNamed:@"Profile_womanIcon"];
    self.vipImage.hidden = model.user.is_vip? NO:YES;
    self.commentLab.text = model.content;
    
    [self.likeButton setTitle:model.like_count forState:UIControlStateNormal];

}

-(void)setFrame:(CGRect)frame{
    
    frame.size.height -= 1;
    
    [super setFrame:frame];
}

@end
